<?php
// @formatter:off
/**
 * @file WCRulesHolder.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-11 21:02:48 +0000 (Thu, 11 Jul 2013) $
 *
 * This file contains definition for 'WCRulesHolder' class.
 *
 * $Id: WCRulesHolder.php 95 2013-07-11 21:02:48Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/includes/holders/WCRulesHolder.php $
 */
// @formatter:on

require_once "{$wcPaths["includes-directory"]}/representations/WCRule.php";

/**
 * @class WCRulesHolder
 * This singleton class is the one in charge of managing all rules. It's purpose
 * is to concentrate all rules fetches in one place avoiding unneeded memory
 * allocation.
 *
 * By default, this class only loads basic information on creation, but no heavy
 * lists. Lists are loaded only where they are needed.
 */
class WCRulesHolder extends WCGHolder {
	/**
	 * @var WCRulesHolder Pointer to the singleton instance.
	 */
	protected static $_Instance;
	/**
	 * @var string Class name of the objects to be held.
	 */
	protected $_PropertiesItemClass = "WCRule";
	/**
	 * @var string This is the prefix of every column on the table
	 * '%rules'.
	 */
	protected $_PropertiesPrefix = "rul_";
	/**
	 * @var string This is the name of the table represented by WCRule
	 * objects. In this case, '%rules'.
	 */
	protected $_PropertiesTable = "rules";
	/**
	 * This constructor sets:
	 *	- Quick accesses to database connection and prefix.
	 *	- Loaded flags to false.
	 *	- An empty list for rules.
	 *
	 * Also calls the database tables check.
	 */
	protected function __construct() {
		parent::__construct();
	}
	/**
	 * Before closing, this destructor clears the rules list.
	 */
	public function __destruct() {
		unset($this->_items);
	}
	/**
	 * Prevent users from clone the singleton's instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}
	//
	// Public methods.

	//
	// Public class methods.
	/**
	 * This class-method creates and returns the sole instance for this
	 * singleton class.
	 *
	 * @return WCRulesHolder Singleton instance.
	 */
	public static function Instance() {
		//
		// Checking if there's no instance yet.
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			//
			// Creating the sole instance.
			self::$_Instance = new $c();
		}
		//
		// Returning the class instance.
		return self::$_Instance;
	}
	/**
	 * This class-method tries to create an object to hold and manage an
	 * action. Such new object depends on it's type and the hooked class for
	 * that type.
	 *
	 * @see $wcRuleHooks
	 *
	 * @param integer $type Code to identificate the right hook.
	 * @param integer $id Action identificator code.
	 * @param WCGroup $group Group assosiated with the requested action.
	 * @return WCRuleAction return the action requested built with its own
	 * class.
	 */
	public static function ActionFactory($actionType, $ruleId, $actionId, WCGroup &$group) {
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Loading global pointers.
		global $wcRuleHooks;
		//
		// Checking if there's a class defined for the action type
		// requested.
		if(isset($wcRuleHooks[$actionType]) && isset($wcRuleHooks[$actionType]["class"]) && class_exists($wcRuleHooks[$actionType]["class"])) {
			//
			// Creating a class shortcut from hooks.
			$c = $wcRuleHooks[$actionType]["class"];
			//
			// Creating an object that will manage the specified
			// action type.
			$out = new $c($ruleId, $actionId, $group);
		} elseif(!isset($wcRuleHooks[$actionType]["class"])) {
			//
			// When an action type is requested and it has a hook, but
			// but such hook has no class specification, it triggers
			// an error.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Action Type '{$actionType}' unknown", E_USER_ERROR);
		} elseif(!class_exists($wcRuleHooks[$actionType]["class"])) {
			//
			// When an action type is requested, it has a hook and a
			// class specification, but such class doesn't exist, it
			// triggers an error.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Action Type '{$actionType}' unknown", E_USER_ERROR);
		} else {
			//
			// When an action type is requested but there's no hook
			// for it, it triggers an error.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Action Type '{$actionType}' unknown", E_USER_ERROR);
		}
		//
		// Returning the action object requested.
		return $out;
	}
}
?>